package com.smsquiztest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Calendar;

import android.os.Environment;

public class AppLogger {

	public static void Write(String message){
		Calendar calendar = Calendar.getInstance();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(calendar.get(Calendar.DATE));
		stringBuilder.append(".");
		stringBuilder.append(calendar.get(Calendar.MONTH));
		stringBuilder.append(".");
		stringBuilder.append(calendar.get(Calendar.YEAR));
		stringBuilder.append(" ");
		stringBuilder.append(calendar.get(Calendar.HOUR_OF_DAY));
		stringBuilder.append(":");
		stringBuilder.append(calendar.get(Calendar.MINUTE));
		stringBuilder.append(":");
		stringBuilder.append(calendar.get(Calendar.SECOND));
		stringBuilder.append("\t");
		stringBuilder.append(message);
		stringBuilder.append("\r\n");
		
		File external = Environment.getExternalStorageDirectory();
		File log = new File(external, "log.txt");
		if(!log.exists()){
			File sd = new File(external, "external_sd");
			log = new File(sd, "log.txt");
		}
		if(log.exists()){
			try {
				PrintWriter logger = new PrintWriter(new FileOutputStream(log, true));				
				logger.println(stringBuilder.toString());
				logger.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}	
	}
	
	public static void WriteError(Exception ex){
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(ex.getMessage());		
		StackTraceElement[] stackTrace = ex.getStackTrace();
		for (int i = 0; i < stackTrace.length; i++) {
			stringBuilder.append("\t");
			stringBuilder.append(stackTrace[i]);
		}
		Write(stringBuilder.toString());
	}
}
